﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace SlideshowGenerator
{
    public partial class Frm_CaptionImageCreator : Form
    {
        private string _captionImagePath = String.Empty;
        string previewImg = String.Empty;

        public Frm_CaptionImageCreator()
        {
            InitializeComponent();
        }

        private void btn_preview_Click(object sender, EventArgs e)
        {
            ShowPreviewImage();
        }

        /// <summary>
        /// Shows a preview image out of the textbox values. 3,375
        /// </summary>
        private void ShowPreviewImage()
        {            
            //create bitmap
            Bitmap bmpp = new Bitmap(Configuration.Constants.ImagePreviewWidth, Configuration.Constants.ImagePreviewHeight);

            DrawImage(bmpp);

            Frm_ImagePreview imgPreview = new Frm_ImagePreview((Image)bmpp);
            imgPreview.ShowDialog(this);
        }

        /// <summary>
        /// Draws background image and the texts of the textboxes on a given image.
        /// </summary>
        /// <param name="image">Destination image with its size for resizing the original image.</param>
        private void DrawImage(Bitmap image)
        {
            Image capImgBG = Properties.Resources.capimgbg;

            //create graphics
            Graphics gp = Graphics.FromImage((Image)image);
            gp.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gp.CompositingQuality = CompositingQuality.HighQuality;
            gp.SmoothingMode = SmoothingMode.HighQuality;
            gp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            //get resizefactor
            int widthdiff = 0;
            int heightdiff = 0;
            double resizefactor = 1;
            if (image.Width > capImgBG.Width)
            {
                widthdiff = image.Width - capImgBG.Width;
                heightdiff = image.Height - capImgBG.Height;

                if (widthdiff > heightdiff)
                {
                    resizefactor += (double)capImgBG.Width / widthdiff;
                }
                else
                {
                    resizefactor += (double)capImgBG.Height / heightdiff;
                }
            }
            else
            {
                widthdiff = capImgBG.Width - image.Width;
                heightdiff = capImgBG.Height - image.Height;

                if (widthdiff > heightdiff)
                {
                    resizefactor -= (double)widthdiff / capImgBG.Width;
                }
                else
                {
                    resizefactor -= (double)heightdiff / capImgBG.Height;
                }
            }

            //resize image and prevent using internal thumbnail
            capImgBG.RotateFlip(RotateFlipType.Rotate180FlipNone);
            capImgBG.RotateFlip(RotateFlipType.Rotate180FlipNone);
            Image bgImageResized = capImgBG.GetThumbnailImage((int)(capImgBG.Width * resizefactor),
                (int)(capImgBG.Height * resizefactor), null, IntPtr.Zero);
            gp.DrawImage(bgImageResized, new Point(0, 0));

            // Create a StringFormat object with the each line of text, and the block
            // of text centered on the page.
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;

            // Draw the caption
            Rectangle rect1 = new Rectangle((int)(100 * resizefactor), (int)(170 * resizefactor), (int)(1920 * resizefactor) - (int)(200 * resizefactor), (int)(100 * resizefactor));
            gp.DrawString(tbx_caption.Text, new Font("Corbel", (int)(85 * resizefactor), FontStyle.Bold), Brushes.Black, rect1, stringFormat);

            //draw content to image
            Rectangle rect2 = new Rectangle((int)(100 * resizefactor), (int)(370 * resizefactor), (int)(1920 * resizefactor) - (int)(200 * resizefactor), (int)(1080 * resizefactor) - (int)(500 * resizefactor));
            gp.DrawString(tbx_content.Text, new Font("Corbel", (int)(71 * resizefactor), FontStyle.Regular), Brushes.Black, rect2, stringFormat);

            //dispose and show image to user
            gp.Dispose();
        }

        /// <summary>
        /// Creates a caption image to a given path.
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns>True, if Caption Image creation was successful.</returns>
        private bool CreateCaptionImage(string imagePath)
        {
            try
            {
                Image capImgBG = Properties.Resources.capimgbg;

                //create a new Bitmap the size of the new image
                Bitmap bmp = new Bitmap(capImgBG.Width, capImgBG.Height);

                DrawImage(bmp);

                //save the image to destination
                bmp.Save(imagePath, ImageFormat.Jpeg);

                return true;
            }
            catch
            {
                return false;
            }
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.CheckPathExists = true;
            saveFileDialog.FileName = Properties.Resources.text_captionImageFileName;
            saveFileDialog.Filter = Properties.Resources.text_jpgFilter;
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.Title = Properties.Resources.text_DirectoryAndNameOfCaptionImage;
            DialogResult dr = saveFileDialog.ShowDialog();

            if (dr == DialogResult.OK)
            {
                bool success = CreateCaptionImage(saveFileDialog.FileName);
                if (success == true)
                {
                    MessageBox.Show(Properties.Resources.text_captionImageCreated, Properties.Resources.mb_Info,
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(Properties.Resources.text_captionImageCreationError, Properties.Resources.mb_Info,
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}
